var app = getApp();

Page({
  data: {
    p: 1,
    collectlist: [],
    isPullDown: false,
    isPullUp: false,
    isLoading: false,
    isPullUpText: ''
  },
  onShow: function () {
    var that = this;
    that.setData({
      p: 1,
      collectlist: [],
      isPullDown: false,
      isPullUp: false,
      isLoading: false,
      isPullUpText: ''
    });
    that.getList();
  },
  onPullDownRefresh: function () {
    var that = this;
    if (that.data.isPullUp || that.data.isPullDown) {
      return
    }
    that.setData({
      p: 1,
      collectlist: [],
      isPullDown: true,
      isPullUp: false,
      isLoading: false,
      isPullUpText: ''
    });
    that.getList();
    wx.stopPullDownRefresh();
  },
  onReachBottom: function () {
    var that = this;
    if (that.data.isPullUp || that.data.isPullDown) {
      return
    }
    that.setData({
      isPullUp: false,
      isPullDown: true,
      isLoading: true
    })
    that.getList();
  },
  getList: function () {
    var that = this;
    app.JsonPost("Api/TripFavorite/getList", {
      p: that.data.p
    }, function (res) {
      if (res.list.length > 0) {
        var l = that.data.collectlist;
        for (var i = 0; i < res.list.length; i++) {
          l.push(res.list[i]);
        }
        that.setData({
          collectlist: l,
          p: that.data.p + 1,
          isPullUp: false,
          isPullDown: false,
          isPullUpText: '上拉加载更多',
        });
      } else {
        that.setData({
          isPullUp: false,
          isPullDown: false,
          isPullUpText: '没有更多数据了'
        });
      }
    });
  },
  collectDelete: function (e) {
    var tripid = e.currentTarget.id;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '点击“确定”将取消收藏该行程',
      showCancel: true,
      success: function (res) {
        if (res.confirm) {
          app.JsonPost("Api/TripFavorite/delete", {
            trip_id: tripid
          }, function (res) {
            console.log(res);
            that.setData({
              p: 1,
              collectlist: [],
              isPullDown: true,
              isPullUp: false,
              isLoading: false,
              isPullUpText: ''
            });
            that.getList();
          })
        }
      }
    });
  },
  detailTrip: function (e) {
    var tripid = e.currentTarget.id;
    wx.navigateTo({
      url: '../../../trip/tripDetail?tripId=' + tripid
    })
  },
  userCredit: function (e) {
    var userid = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '../../../user/credit?userId=' + userid
    })
  }
})